<?php

/**
 * Class to generate metaxml
 */

class Meta {
	public $cr = "\\r\\n";
	public $delimiter = ",";
	
	public $taxonList = array(
			'scientificName'
		, 'parentNameUsageID'
		, 'acceptedNameUsageID'
		, 'scientificNameAuthorship'
		, 'nomenclaturalCode'
		, 'taxonRank'
		, 'nameAccordingTo'
		, 'taxonRemarks'
	);
	
	public $occurrenceList = array(
			'institutionCode'
		,	'collectionID'
		,	'collectionCode'
		,	'catalogNumber'
		,	'sex'
		,	'kingdom'
		,	'phylum'
		,	'class'
		,	'order'
		,	'family'
		,	'genus'
		,	'subgenus'
		,	'specificEpithet'
		,	'infraspecificEpithet'
		,	'scientificName'
		,	'scientificNameAuthorship'
		,	'taxonRank'
		,	'dateIdentified'
		,	'identifiedBy'
		,	'typeStatus'
		,	'continent'
		,	'waterBody'
		,	'country'
		,	'stateProvince'
		,	'locality'
		,	'decimalLatitude'
		,	'decimalLongitude'
		,	'coordinatePrecision'
		,	'minimumElevationInMeters'
		,	'maximumElevationInMeters'
		,	'minimumDepthInMeters'
		,	'maximumDepthInMeters'
		,	'basisOfRecord'
		,	'eventDate'
		,	'year'
		,	'month'
		,	'day'
		,	'habitat'
		,	'fieldNumber'
		,	'recordedBy'
		,	'samplingProtocol'
		,	'associatedMedia'
		,	'eventRemarks'
		,	'modified'
	);
/*
		<id index="0"/>
		<field  index="1" term="http://rs.tdwg.org/dwc/terms/institutionCode"/>
		<field  index="2" term="http://rs.tdwg.org/dwc/terms/collectionID"/>
		<field  index="3" term="http://rs.tdwg.org/dwc/terms/catalogNumber"/>
		<field  index="4" term="http://rs.tdwg.org/dwc/terms/sex"/>
		<field  index="5" term="http://rs.tdwg.org/dwc/terms/kingdom"/>
		<field  index="6" term="http://rs.tdwg.org/dwc/terms/phylum"/>
		<field  index="7" term="http://rs.tdwg.org/dwc/terms/class"/>
		<field  index="8" term="http://rs.tdwg.org/dwc/terms/order"/>
		<field  index="9" term="http://rs.tdwg.org/dwc/terms/family"/>
		<field  index="10" term="http://rs.tdwg.org/dwc/terms/genus"/>
		<field  index="11" term="http://rs.tdwg.org/dwc/terms/subgenus"/>
		<field  index="12" term="http://rs.tdwg.org/dwc/terms/specificEpithet"/>
		<field  index="13" term="http://rs.tdwg.org/dwc/terms/infraspecificEpithet"/>
		<field  index="14" term="http://rs.tdwg.org/dwc/terms/scientificName"/>
		<field  index="15" term="http://rs.tdwg.org/dwc/terms/scientificNameAuthorship"/>
		<field  index="16" term="http://rs.tdwg.org/dwc/terms/taxonRank"/>
		<field  index="17" term="http://rs.tdwg.org/dwc/terms/dateIdentified"/>
		<field  index="18" term="http://rs.tdwg.org/dwc/terms/identifiedBy"/>
		<field  index="19" term="http://rs.tdwg.org/dwc/terms/typeStatus"/>
		<field  index="20" term="http://rs.tdwg.org/dwc/terms/continent"/>
		<field  index="21" term="http://rs.tdwg.org/dwc/terms/waterBody"/>
		<field  index="22" term="http://rs.tdwg.org/dwc/terms/country"/>
		<field  index="23" term="http://rs.tdwg.org/dwc/terms/stateProvince"/>
		<field  index="24" term="http://rs.tdwg.org/dwc/terms/locality"/>
		<field  index="25" term="http://rs.tdwg.org/dwc/terms/decimalLatitude"/>
		<field  index="26" term="http://rs.tdwg.org/dwc/terms/decimalLongitude"/>
		<field  index="27" term="http://rs.tdwg.org/dwc/terms/coordinatePrecision"/>
		<field  index="28" term="http://rs.tdwg.org/dwc/terms/minimumElevationInMeters"/>
		<field  index="29" term="http://rs.tdwg.org/dwc/terms/maximumElevationInMeters"/>
		<field  index="30" term="http://rs.tdwg.org/dwc/terms/minimumDepthInMeters"/>
		<field  index="31" term="http://rs.tdwg.org/dwc/terms/maximumDepthInMeters"/>
		<field  index="32" term="http://rs.tdwg.org/dwc/terms/basisOfRecord"/>
		<field  index="33" term="http://rs.tdwg.org/dwc/terms/eventDate"/>
		<field  index="34" term="http://rs.tdwg.org/dwc/terms/year"/>
		<field  index="35" term="http://rs.tdwg.org/dwc/terms/month"/>
		<field  index="36" term="http://rs.tdwg.org/dwc/terms/day"/>
		<field  index="37" term="http://rs.tdwg.org/dwc/terms/habitat"/>
		<field  index="38" term="http://rs.tdwg.org/dwc/terms/fieldNumber"/>
		<field  index="39" term="http://rs.tdwg.org/dwc/terms/recordedBy"/>
		<field  index="41" term="http://rs.tdwg.org/dwc/terms/associatedMedia"/>
		<field  index="42" term="http://rs.tdwg.org/dwc/terms/eventRemarks"/>
*/
	public $distributionHeaderList = array(
			'http://rs.tdwg.org/dwc/terms/countryCode'
		, 'http://iucn.org/terms/threatStatus'
		, 'http://rs.tdwg.org/dwc/terms/eventDate'
		, 'http://rs.tdwg.org/dwc/terms/establishmentMeans'
	);

	public $vernacularHeaderList = array(
		'vernacularName'
	);

	public function __construct() {}

	public function generateMetaData( $indexes = array("species" => 1, "synonyms" => 1, "vernacular" => 1, "distribution" => 1), $filenames, $coreType = "taxon" ) {
		
		switch( strtolower($coreType) ) {
			case 'occurrence':
				$coreList = $this->occurrenceList;
				$prefix = 'http://rs.tdwg.org/dwc/terms/';
				$rowType = 'http://rs.tdwg.org/dwc/terms/Occurrence';
				$coreFilename = 'occurrence.csv';
				break;

			case 'taxon':
			default:
				$coreList = $this->taxonList;
				$prefix = 'http://rs.tdwg.org/dwc/terms/';
				$rowType = 'http://rs.tdwg.org/dwc/terms/Taxon';
				$coreFilename = 'checklist.csv';
				break;
		}

		$this->xml = new XMLWriter();
		$this->xml->openMemory();
		$this->xml->startDocument('1.0', 'UTF-8');
		$this->xml->setIndent(4);
			$this->xml->startElement('archive');
			$this->xml->writeAttribute('xmlns', 'http://rs.tdwg.org/dwc/text/');
			$this->xml->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
			$this->xml->writeAttribute('xsi:schemaLocation', 'http://rs.tdwg.org/dwc/text/ http://rs.tdwg.org/dwc/text/tdwg_dwc_text.xsd');
			$this->xml->writeAttribute('metadata', 'eml.xml');

				$this->xml->startElement('core');
					$this->xml->writeAttribute('encoding', 'UTF-8');
					$this->xml->writeAttribute('linesTerminatedBy', $this->cr);
					$this->xml->writeAttribute('fieldsTerminatedBy', $this->delimiter);
					$this->xml->writeAttribute('fieldsEnclosedBy', '"');
					$this->xml->writeAttribute('ignoreHeaderLines', '1');
					$this->xml->writeAttribute('rowType', $rowType);

					$this->xml->startElement('files');
						$this->xml->writeElement('location', $coreFilename);
					$this->xml->endElement();
	
					$this->xml->startElement('id');
						$this->xml->writeAttribute('index', 0);
					$this->xml->endElement();
					for($i=0; $i< count($coreList); $i++) {
						$this->xml->startElement('field');
							$this->xml->writeAttribute('index', $i+1);
							if ($coreList[$i] == "modified") {								
								$this->xml->writeAttribute('term', "http://purl.org/dc/terms/" . $coreList[$i]);
							} else {
								$this->xml->writeAttribute('term', $prefix . $coreList[$i]);
							}
						$this->xml->endElement();
					}
				$this->xml->endElement();

		if ($indexes["distribution"] > 1) {
				$this->xml->startElement('extension');
					$this->xml->writeAttribute('encoding', 'UTF-8');
					$this->xml->writeAttribute('linesTerminatedBy', $this->cr);
					$this->xml->writeAttribute('fieldsTerminatedBy', $this->delimiter);
					$this->xml->writeAttribute('fieldsEnclosedBy', '"');
					$this->xml->writeAttribute('ignoreHeaderLines', '1');
					$this->xml->writeAttribute('rowType', 'http://rs.gbif.org/terms/1.0/Distribution');

					$this->xml->startElement('files');
						$this->xml->writeElement('location', 'distribution.csv');
					$this->xml->endElement();
	
					$this->xml->startElement('coreid');
						$this->xml->writeAttribute('index', 0);
					$this->xml->endElement();
					for($i=0; $i< count($this->distributionHeaderList); $i++) {
						$this->xml->startElement('field');
							$this->xml->writeAttribute('index', $i+1);
							$this->xml->writeAttribute('term', $this->distributionHeaderList[$i]);
						$this->xml->endElement();
					}
				$this->xml->endElement();
		}

		if ($indexes["vernacular"] > 1) {
				$this->xml->startElement('extension');
					$this->xml->writeAttribute('encoding', 'UTF-8');
					$this->xml->writeAttribute('linesTerminatedBy', $this->cr);
					$this->xml->writeAttribute('fieldsTerminatedBy', $this->delimiter);
					$this->xml->writeAttribute('fieldsEnclosedBy', '"');
					$this->xml->writeAttribute('ignoreHeaderLines', '1');
					$this->xml->writeAttribute('rowType', 'http://rs.gbif.org/terms/1.0/VernacularName');

					$this->xml->startElement('files');
						$this->xml->writeElement('location', 'vernacular.csv');
					$this->xml->endElement();
	
					$this->xml->startElement('coreid');
						$this->xml->writeAttribute('index', 0);
					$this->xml->endElement();
					for($i = 0; $i < count($this->vernacularHeaderList); $i++) {
						$this->xml->startElement('field');
							$this->xml->writeAttribute('index', $i+1);
							$this->xml->writeAttribute('term', 'http://rs.tdwg.org/dwc/terms/' . $this->vernacularHeaderList[$i]);
						$this->xml->endElement();
					}
				$this->xml->endElement();
		}

			$this->xml->endElement();
		$this->xml->endDocument();
//		return( str_replace('"&quot;"', '\'"\'', $this->xml->outputMemory()) );
		return( $this->xml->outputMemory() );
	}
	
}


?>